/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.persistence.repository.collection;

import java.util.*;

/**
 * List implementation of {@link org.metalframework.persistence.repository.collection.PersistentCollection}.
 * 
 * @param <T> Type of the entity whose list represents the extent.
 * 
 * @since 1.0
 * @see org.metalframework.persistence.repository.collection.PersistentCollection
 * @see org.metalframework.persistence.repository.collection.PersistentCollectionStrategy
 */
public class PersistentList<T> extends AbstractList<T> implements PersistentCollection<T> {

	private static final long serialVersionUID = 6787060265721449477L;
	
	/**
	 * Strategy to use to interact with the repository.
	 */
	private PersistentCollectionStrategy<T> strategy;
	
	/**
	 * Creates a persistent list with no strategy.
	 */
	public PersistentList() {
		this(null);
	}
	
	/**
	 * Creates a persistent list with the specified strategy.
	 * 
	 * @param strategy Strategy to interact with the repository.
	 */
	public PersistentList(PersistentCollectionStrategy<T> strategy) {
		this.strategy = strategy;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void add(int index, T element) {
		strategy.add(index, element);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public T get(int index) {
		return strategy.get(index);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public T set(int index, T element) {
		return strategy.set(index, element);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public T remove(int index) {
		return strategy.get(index);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int size() {
		return strategy.size();
	}

	/**
	 * {@inheritDoc}
	 */
	public void flush() {
		strategy.flush();
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Class<T> getEntityType() {
		return strategy.getEntityType();
	}
}
